package cn.gson.crmdemo.model.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;

import cn.gson.crmdemo.model.dao.CustomerDao;
import cn.gson.crmdemo.model.dao.SalesLogDao;
import cn.gson.crmdemo.model.dao.SalesOpportunityDao;
import cn.gson.crmdemo.model.mapper.SalesOpportunityMapper;
import cn.gson.crmdemo.model.pojo.Customer;
import cn.gson.crmdemo.model.pojo.Dept;
import cn.gson.crmdemo.model.pojo.Emp;
import cn.gson.crmdemo.model.pojo.Saleslog;
import cn.gson.crmdemo.model.pojo.Salesopportunity;
import cn.gson.crmdemo.model.vo.EmpVo;
import cn.gson.crmdemo.model.vo.ReturndetailedReturnProductVo;
import cn.gson.crmdemo.model.vo.SaleVo;
import cn.gson.crmdemo.model.vo.SaleslogVo;

@Service
@Transactional(rollbackFor=Exception.class)
public class SalesService {
	@Autowired
	SalesOpportunityDao sodao;
	@Autowired
	SalesOpportunityMapper som;
	@Autowired
	CustomerDao cdao;
	@Autowired
	SalesLogDao sldao;
	/**
	 * 查所有
	 * @return
	 */
	public JSONArray selectSalesopportunity(){
		return som.selectSalesopportunity();
	}
	/**
	 * 条件查询
	 * @return
	 */
	public JSONArray selectState(Integer saleState){
		return som.selectState(saleState);
	}
	/**
	 * 查询销售员
	 * @return
	 */
	public JSONArray selectSaleMan(){
		return som.selectSaleMan();
	}
	/**
	 * 新增
	 * @param dept 由用户提交到Controller
	 */
	public void addSales(SaleVo sale) {
		Salesopportunity sales = new Salesopportunity();
		sales.setSaleTheme(sale.getSaleTheme());
		sales.setSaleEmpName(sale.getSaleEmpName());
		Customer cusId = cdao.findById(sale.getCustomerId()).get();
		
		sales.setSaleDate(sale.getSaleDate());
		sales.setSaleRemark(sale.getSaleRemark());
		sales.setSaleSource(sale.getSaleSource());
		sales.setSaleState(sale.getSaleState());
		System.err.println("ser==========="+sale.getSaleId()+sale.getSaleTheme()+sale.getSaleDate());
		sales.setCustomer(cusId);
		sodao.save(sales);
	}
	/**
	 * 查询销售最大id
	 * @return
	 */
	public JSONArray selectMaxId() {
		return som.selectSaleMax();
	}
	/**
	 * 添加销售机会日志
	 * @param 
	 */
	public void addSalesLog(SaleslogVo s) {
		sldao.save(s);
	}
	public void addSalesLogs(SaleslogVo s) {
		Saleslog sl = new Saleslog();
		sl.setLogDate(s.getLogDate());
		sl.setLogDescribe(s.getLogDescribe());
		sl.setLogEmpName(s.getLogEmpName());
		sl.setLogMoney(s.getLogMoney());
		sl.setLogRemark(s.getLogRemark());
		sl.setLogType(s.getLogType());
		sl.setSaleId(s.getSalesopportunity());
		sldao.save(s);
	}
	/**
	 * 删除
	 */
	public void deleteSales(Integer saleId) {
		//先查询对象
		som.delSaleId(saleId);
	}
	
	/**
	 * 修改员工信息
	 */
	public void updateSales(SaleVo sale) {
		Customer cusId = cdao.findById(sale.getCustomerId()).get();
		
		Salesopportunity sales = sodao.findById(sale.getSaleId()).get();
		sales.setSaleTheme(sale.getSaleTheme());
		sales.setSaleEmpName(sale.getSaleEmpName());
		
		sales.setSaleDate(sale.getSaleDate());
		sales.setSaleRemark(sale.getSaleRemark());
		sales.setSaleSource(sale.getSaleSource());
		sales.setSaleState(sale.getSaleState());

		sales.setCustomer(cusId);
		
	}
	
	public JSONArray selectSaleId(Integer saleId) {
		return som.selectSaleId(saleId);
	}
	
	public JSONArray selectSaleTheme(String saleTheme){
		return som.selectSaleTheme(saleTheme);
	}
	/**
	 * 查所有id和名称
	 * @return
	 */
	public JSONArray selectSaleName(Integer customerId){
		return som.selectSaleName(customerId);
	}
	
	
	
}
